//时间格式化
export function dateFormat(d:any, fmt:string = 'yyyy-MM-dd hh:mm:ss'):string {
	if (typeof d == 'string') {
		if (d.length == 10) d = d + '000'
		d = Number(d)
	}
	if (typeof d == 'number') {
		if (d.toString().length == 10) d = d * 1000
		d = new Date(d)
	}
	var o = {
		"M+": d.getMonth() + 1, //月份
		"d+": d.getDate(), //日
		"h+": d.getHours(), //小时
		"m+": d.getMinutes(), //分
		"s+": d.getSeconds(), //秒
		"q+": Math.floor((d.getMonth() + 3) / 3), //季度
		"S": d.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

/**
 * 获取随机字符串
 * @param {*} len 字符串位数
 */
export function randomString(len:number):string {
	len = len || 32;
	var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
	var maxPos = $chars.length;
	var pwd = '';
	for (let i = 0; i < len; i++) {
		pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
	}
	return pwd;
}

/**
 * 检测路由权限
 * @param {object} route
 */
export function checkAccess(route, user) {
	if (route.meta.noaccess) {
		return true
	} else {
		return user.access.includes(route.name)
	}
}


/**
 * 检测图片宽高
 * @param {object} file
 */
export function getImageWidthAndHeight(file:File):Promise<{width:number,height:number}> {
	return new Promise((resolve) => {
		let img = new Image();
		let content = URL.createObjectURL(file)
		img.onload = function () {
			resolve({
				width: img.width,
				height: img.height
			});
		};
		img.src = content;
	})
}
